var app = angular.module('ZJPC');

app.controller('IcardController', ['$scope', '$state','$uibModalInstance', '$http', 'DataService', 'data', 'CesiumService', '$filter', 'ModalService', 
  function($scope, $state, $uibModalInstance, $http, DataService, data, CesiumService, $filter,ModalService) {


    $scope.cancel = function() {
      $uibModalInstance.dismiss('cancel');
    };
    $scope.loading = true;
    var getIcardTree = DataService.deepCopy(getTree);
    $scope.treeData = getIcardTree.ykt;

    if (data.checked){
      var num = data.checked.substr(3);
    }else{
      var num = data.substr(3);
    }

    $scope.treeData[setStatus(num)];
    $scope.treeConfig = {
          core : {
              multiple : true,
              animation: true,
              error : function(error) {
                  $log.error('treeCtrl: error from js tree - ' + angular.toJson(error));
              },
              check_callback : true,
              worker : true
          },
          types : {
              default : {
                  icon : 'glyphicon glyphicon-flash'
              },
              star : {
                  icon : '/img/gather.png'
              },
              cloud : {
                  icon : '/img/icard_ico.png'
              }
          },
          version : 1,
          plugins : ['types','checkbox']
    };

    function setStatus (id){
      if(id == '#'){
        return;
      }
      for(var i = 0; i < $scope.treeData.length; i ++){
        if($scope.treeData[i].id == id){
          $scope.treeData[i].state.opened = true;
          if($scope.treeData[i].type == 'cloud'){
            $scope.treeData[i].state.selected = true;
          }
          setStatus($scope.treeData[i].parent);
          return;
        }
      }
    }

    $scope.pageOptions = {
        pageSize:15,
        itemCount:0,
        current:0
    }


    $scope.endtime = new Date();
    $scope.starttime = new Date('2018/01/04');
    $scope.starttime.setHours(0,0,0);
    $scope.endtime.setHours(23,59,59);
    getData(num, $scope.starttime, $scope.endtime, $scope.pageOptions.current, $scope.pageOptions.pageSize);

    var bm = num;
    $scope.readyCB = function (e, m){
      for(var i = 0; i < m.selected.length; i++){
        if(m.selected[i] == 'ajson-2'){
          bm = m.node.children;
        }else{
          bm = m.selected;
        }
      }
    }

    $scope.uncheck = function (e, m){
      bm = m.selected;
    }

    $scope.searchCard = function (){
      $scope.loading = true;
      $scope.pageOptions.current = 0;
      getData(bm, $scope.starttime, $scope.endtime, $scope.pageOptions.current, $scope.pageOptions.pageSize);
    }



    $scope.getData = function(item){
      getData(bm, $scope.starttime, $scope.endtime, $scope.pageOptions.current, $scope.pageOptions.pageSize);
    }

    
    function getData (param, starttime, endtime, page, pageNum){
      var starttime = $filter('date')(starttime, 'yyyy-MM-dd HH:mm:ss');
      var endtime = $filter('date')(endtime, 'yyyy-MM-dd HH:mm:ss');
      DataService.restGetApi('school/track/getIcard?jid=&&list=' + param + '&&starttime=' + starttime + '&&endtime=' + endtime + '&&page=' + page + '&&pageNum=' + pageNum).then(function (icard_data){
        $scope.loading = false;
        $scope.icardData = icard_data.data.data;
        $scope.pageOptions.itemCount = icard_data.data.total;
      },function (err){
        console.log(err);
      });
    }


    $scope.openIt = function(data){
      if(data.ly=='ykt_business'){
         ModalService.openModalBySize('../templates/region/detail/jydetail.html', 'IcardDetailController', data,'minisize')
      }else if(data.ly=='ykt_mj'){
        ModalService.openModalBySize('../templates/region/detail/mjdetail.html', 'IcardDetailController', data,'minisize')
      }else if(data.ly=='ykt_library'){
        ModalService.openModalBySize('../templates/region/detail/librarydetail.html', 'IcardDetailController', data,'minisize')
      }else if(data.ly=='ykt_locker_use'){
         ModalService.openModalBySize('../templates/region/detail/gzdetail.html', 'IcardDetailController', data,'minisize')
      }else{
        ModalService.openModalBySize('../templates/region/detail/cgdetail.html', 'IcardDetailController', data,'minisize')
      }
    };

    
    /*时间控件校验相关设置*/
    $scope.endDateBeforeRender = endDateBeforeRender
    $scope.endDateOnSetTime = endDateOnSetTime
    $scope.startDateBeforeRender = startDateBeforeRender
    $scope.startDateOnSetTime = startDateOnSetTime

    function startDateOnSetTime () {
      $scope.$broadcast('start-date-changed');
    }

    function endDateOnSetTime () {
      $scope.$broadcast('end-date-changed');
    }

    function startDateBeforeRender ($dates) {
      if ($scope.endtime) {
        $scope.endtime = new Date($scope.endtime).setHours(23,59,59)
        var activeDate = moment($scope.endtime);

        $dates.filter(function (date) {
          return date.localDateValue() >= activeDate.valueOf()
        }).forEach(function (date) {
          date.selectable = false;
        })
      }
    }

    function endDateBeforeRender ($view, $dates) {
      if ($scope.starttime) {
        var activeDate = moment($scope.starttime).subtract(1, $view).add(1, 'minute');

        $dates.filter(function (date) {
          return date.localDateValue() <= activeDate.valueOf()
        }).forEach(function (date) {
          date.selectable = false;
        })
      }
    }

}]);